﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class email_template_service : base_service
    {
        //get all get all
        public PagedList<email_template> email_template_plist(int pageindex, int pagesize )
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_email_template
                            select dt);
                list = list.OrderBy(p => p.email_order);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        public int email_template_update(email_template it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_email_template.Where(p => p.email_id == it.email_id).FirstOrDefault();
                if (item != null)
                {
                    item.email_id = it.email_id;
                    item.email_name = it.email_name;
                    item.email_subject = it.email_subject;
                    item.email_content = it.email_content;
                    item.email_order = it.email_order;
                    item.modified = Udate_time.GetCurrentDateTime(); 
                    item.email_status = it.email_status;
                    item.modified_by = it.modified_by; 

                    data.SaveChanges();
                    return item.email_id;
                }
                else
                {
                    data.AddTotbl_email_template(it);
                    data.SaveChanges();
                    return it.email_id;
                }
            }
        }
        //enable - disable  
        public int updateStatus(int id, byte status)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_email_template.Where(p => p.email_id == id).FirstOrDefault();

                if (edit_item != null) {
                    edit_item.email_status = status;
                    rt = email_template_update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public email_template getByID(int id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_email_template.Where(p => p.email_id == id).FirstOrDefault();
               // if (item == null)
                //    item = new email_templates();
                return item;
            }
        }

        //delete by id
        public void deleteItem(int id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_email_template.Where(p => p.email_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
